<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>指定需要被序列化成 JSON 的数据</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.jsonserializable.html">« JsonSerializable</a></li>
      <li style="float: right;"><a href="ref.json.html">JSON 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.jsonserializable.html">JsonSerializable</a></li>
    <li>指定需要被序列化成 JSON 的数据</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="jsonserializable.jsonserialize" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">JsonSerializable::jsonSerialize</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">JsonSerializable::jsonSerialize</span> &mdash; <span class="dc-title">指定需要被序列化成 JSON 的数据</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-jsonserializable.jsonserialize-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>JsonSerializable::jsonSerialize</strong></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   序列化物体（Object）成能被 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 原生地序列化的值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-jsonserializable.jsonserialize-parameters">
  <h3 class="title">参数</h3>
  <p class="para">此函数没有参数。</p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-jsonserializable.jsonserialize-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回能被 <span class="function"><a href="function.json-encode.html" class="function">json_encode()</a></span> 序列化的数据，
   这个值可以是除了 <a href="language.types.resource.html" class="link">资源(resource)</a> 外的任意类型。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-jsonserializable.jsonserialize-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3405">
    <p><strong>示例 #1 
     返回 <span class="type">array</span> 的 <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ArrayValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ArrayValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
[
    1,
    2,
    3
]
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3406">
    <p><strong>示例 #2 
     返回关联 <span class="type">array</span> 的 <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">ArrayValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(array&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">array</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$array&nbsp;</span><span style="color: #007700">=&nbsp;[</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'quux'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">];<br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">ArrayValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
{
    &quot;foo&quot;: &quot;bar&quot;,
    &quot;quux&quot;: &quot;baz&quot;
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3407">
    <p><strong>示例 #3 
     返回 <span class="type">int</span> 的 <span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子s
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">IntegerValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">number&nbsp;</span><span style="color: #007700">=&nbsp;(integer)&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">number</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">IntegerValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
1
</pre></div>
    </div>
   </div>
   <div class="example" id="example-3408">
    <p><strong>示例 #4 
     返回 <span class="type">string</span> 的<span class="methodname"><strong>JsonSerializable::jsonSerialize()</strong></span> 例子
    </strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">StringValue&nbsp;</span><span style="color: #007700">implements&nbsp;</span><span style="color: #0000BB">JsonSerializable&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">string&nbsp;</span><span style="color: #007700">=&nbsp;(string)&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">jsonSerialize</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />echo&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">StringValue</span><span style="color: #007700">(</span><span style="color: #DD0000">'Hello!'</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">JSON_PRETTY_PRINT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&quot;Hello!&quot;
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div></div></div></body></html>